package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.miaodong.entity.AppRoomMeili;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 房间背景
 */
@Repository
public class AppRoomMeiliDao extends BaseDao<AppRoomMeili> {

    /**
     * 获取用户在该房间内的活跃度
     * @param userid
     * @param roomid
     * @return
     */
    public AppRoomMeili getMeili(Integer userid,Integer roomid) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room_meili where 1=1  ");
        sbStr.append(" and userid = ? ");
        paramList.add(userid);
        sbStr.append(" and roomid = ? ");
        paramList.add(roomid);
        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, AppRoomMeili.class);
    }

    /**
     * 获取房间的活跃度
     * @param roomid
     * @return
     */
    public AppRoomMeili getRoomMeili(Integer roomid,Integer userId) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_room_meili where 1=1");
        sbStr.append(" and roomid = ?");
        paramList.add(roomid);
        sbStr.append(" and userid = ?");
        paramList.add(userId);
        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, AppRoomMeili.class);
    }


    /**
     * 添加/修改
     */
    public void save(AppRoomMeili haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 清空该房间内所有人的魅力值
     */
    public void delMeili(Integer roomid) {
        if (roomid <= 0) {
            return;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer("UPDATE app_room_meili SET meilizhi  = 0 ");
        sql.append(" WHERE roomid = ? ");
        paramList.add(roomid);
        this.update(sql.toString(),paramList);
    }



    /**
     * 增加活跃度
     * @param userid 用户id
     * @param roomid 房间id
     * @param money 魅力值
     */
    public void addMeili(Integer userid,Integer roomid,long money){
        if(StringEx.isNullOrLing(userid)){
            return;
        }
        if(StringEx.isNull(roomid)){
            return;
        }
        if(StringEx.isNull(money)){
            return;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuffer sql = new StringBuffer(" UPDATE app_room_meili SET meilizhi  = meilizhi + ? ");
        paramList.add(money);
        sql.append(" WHERE userid = ? ");
        paramList.add(userid);
        sql.append(" and roomid = ? ");
        paramList.add(roomid);
        this.update(sql.toString(),paramList);
    }

}
